package com.epam.tcs.dao.hibernate.impl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.epam.tcs.dao.factory.ProgramDAO;
import com.epam.tcs.dao.hibernate.ConnectionFactory;
import com.epam.tcs.dao.hibernate.entities.Program;

public class ProgramDAOHibernateImpl extends ElementDAOHibernateImpl<Program>
		implements ProgramDAO {

	public ProgramDAOHibernateImpl(Class<?> objType) {
		this.objType = objType;
	}

	@Override
	public Program getElement(long id) {
		Session session = ConnectionFactory.getOrInitSession();
		Program program = (Program) session.load(objType, id);

		Hibernate.initialize(program.getThemes());

		session.close();
		return program;
	}

	@Override
	public List<Program> getAllElements() {
		Session session = ConnectionFactory.getOrInitSession();
		List<Program> programs = (List<Program>)session.createQuery("from "+objType.getName()).list();
		
		for (Program program : programs) {
			Hibernate.initialize(program.getThemes());
		}
		
		session.close();
		return programs;
	}

}
